package cn.daomeng.firebird.core.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

import cn.daomeng.firebird.core.environment.CalorieEnvironment;
import cn.daomeng.firebird.core.log.ILogService;
import cn.daomeng.firebird.core.log.LogDefaultServiceImpl;
import cn.daomeng.firebird.core.log.LogFactory;
import cn.daomeng.firebird.core.log.LogProdServiceImpl;
import net.bull.javamelody.MonitoringFilter;

/**
 * 
 * 
 * @author wf.shu
 * @since 2019-03-04 11:13
 * @version 1.0.0.0
 */
@Configuration
@PropertySource(value={"classpath:calorie-core.properties"},ignoreResourceNotFound = true,encoding = "UTF-8")
public class BeanConfiguration {
	/**
	 * 系统运行环境默认值将被设置成PRODUCT
	 */
	@Value("${calorie.environment: product}")
	private String environment;
	/**
	 * 初始化系统运行环境
	 * 
	 * @author wf.shu
	 * @since 2019年3月14日 上午9:53:09
	 */
	@Bean
	public CalorieEnvironment initCalorieEnvironment() {
		return new CalorieEnvironment(StringUtils.upperCase(environment));
	}

	/**
	 * 根据系统环境初始化相应的ILogService相应内容
	 * 
	 * @author wf.shu
	 * @since 2019年3月14日 上午9:51:57
	 */
	@Bean
	public ILogService logService(CalorieEnvironment calorieEnvironment) {
		if (calorieEnvironment.isProduct()) {
			LogFactory.logService = new LogProdServiceImpl();
		}
		if (null == LogFactory.logService) {
			LogFactory.logService = new LogDefaultServiceImpl();
		}
		return LogFactory.logService;
	}

	/**
	 * 增加springboot 对于JSR303的扩展（结局JSR303默认不支持方法参数校验的问题）
	 * @author Qin.Siliang 
	 * @since 2019/3/7 
	 * @return
	 */
	@Bean
	public MethodValidationPostProcessor methodValidationPostProcessor() {
		return new MethodValidationPostProcessor();
	}
	
	@Bean
	public FilterRegistrationBean monitorFilter() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MonitoringFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		return filterRegistrationBean;
	}
	
	
}
